package com.hehe.controller;

import com.hehe.POJO.User;
import com.hehe.Repository.UserRepository;
import jakarta.annotation.Resource;
import jakarta.annotation.Resources;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.ServiceLoaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Controller
public class LoginController {


    @Value("${config_file_name}")
    private String config_file_name;


    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserRepository userRepository;

    @GetMapping("/login")
    public String loginPage(HttpServletRequest request) {
        System.out.println("开始登录...");
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("username".equals(cookie.getName())) {
                    log.info("用户已登录，继续处理请求");
                    return "login/login-success.html";
                }
            }
        }
        return "login/login.html";
    }




//    @ModelAttribute("loggedInUser")
//    public Tb_user getLoggedInUser(@CookieValue(value = "username", required = false) String username) {
//        if (username != null) {
//            // Simulate fetching user data from a service or database
//            Tb_user tb_user = new Tb_user();
//            tb_user.setUsername(username);
//            return tb_user;
//        }
//        return null;
//    }


    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, HttpServletResponse response, Model model) {
        User tb_user = userRepository.findByUsername(username);
        if (tb_user != null && passwordEncoder.matches(password, tb_user.getPassword())) { // 使用 BCrypt 验证密码
            Cookie cookie = new Cookie("username", tb_user.getUsername());
            cookie.setMaxAge(15 * 24 * 60 * 60); // 15 days
            response.addCookie(cookie);
            return "redirect:/login-success";
        }
        model.addAttribute("error", "用户名 或 密码 错误");
        return "login/login.html";
    }


    @GetMapping("/login-success")
    public String homePage(HttpServletRequest request) {
//        Cookie[] cookies = request.getCookies();
//        if (cookies != null) {
//            for (Cookie cookie : cookies) {
//                if ("username".equals(cookie.getName())) {
//                    System.out.println("用户已登录，继续处理请求");
//                    return "login-success.html";
//                }
//            }
//        }
//        return "login.html";
        return "login/login-success.html";
    }


    @GetMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("username".equals(cookie.getName())) {
                    cookie.setValue(null);
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }
        return "redirect:/";
    }


}



